import { ref, nextTick } from 'vue';
import { computeWordCount, resolveTagFromNode, scrollToCursor as scrollToCursorUtil } from '../utils';

/**
 * 编辑器核心功能
 */
export function useEditorCore(props, emit) {
  const editorInstance = ref(null);
  const isEditorReady = ref(false);
  const editorHasFocus = ref(false);
  const showCustomPlaceholder = ref(true);
  const currentTag = ref('P');
  const wordCount = ref(0);

  /**
   * 更新字数统计
   */
  const updateWordCount = () => {
    if (!editorInstance.value) return;
    try {
      const text = editorInstance.value.getBody().textContent || '';
      wordCount.value = computeWordCount(text);
    } catch (e) {
      wordCount.value = 0;
    }
  };

  /**
   * 更新当前标签
   */
  const updateCurrentTag = () => {
    if (!editorInstance.value) return;
    try {
      const selection = editorInstance.value.selection;
      if (!selection) return;
      const node = selection.getNode();
      currentTag.value = resolveTagFromNode(node);
    } catch (e) {
      currentTag.value = 'P';
    }
  };

  /**
   * 自动滚动到光标位置
   */
  const scrollToCursor = () => {
    if (!editorInstance.value) return;
    scrollToCursorUtil(editorInstance.value);
  };

  /**
   * 更新自定义 placeholder 显示状态
   */
  const updateCustomPlaceholder = (aiState) => {
    if (!editorInstance.value) return;
    
    const body = editorInstance.value.getBody();
    const textContent = (body.textContent || '').trim();
    const isEmpty = textContent === '' || textContent === '\u00A0'; // 包括 &nbsp;
    
    // 检查是否有多个块级元素（即使为空，说明用户已在编辑）
    const blockElements = body.querySelectorAll('p, div, h1, h2, h3, h4, h5, h6, li, blockquote, pre');
    const hasMultipleBlocks = blockElements.length > 1;
    
    // 快速退出：有内容或多个块，不显示
    if (!isEmpty || hasMultipleBlocks) {
      showCustomPlaceholder.value = false;
      return;
    }
    
    // 内容为空且只有一个块的情况
    if (aiState && editorHasFocus.value) {
      // AI 启用且有焦点：不显示默认 placeholder，让 AI placeholder 显示
      showCustomPlaceholder.value = false;
    } else {
      // 其他情况：显示默认 placeholder
      showCustomPlaceholder.value = true;
    }
  };

  /**
   * 处理内容点击
   */
  const handleContentClick = (updateCustomPlaceholderFn, updateCurrentTagFn, updateAiMarkerFn) => {
    updateCustomPlaceholderFn();
    updateCurrentTagFn();
    updateAiMarkerFn();
  };

  /**
   * 处理内容变化
   */
  const handleContentChange = (updateCustomPlaceholderFn, updateWordCountFn, updateCurrentTagFn, updateAiMarkerFn) => {
    updateCustomPlaceholderFn();
    updateWordCountFn();
    updateCurrentTagFn();
    updateAiMarkerFn();
  };

  /**
   * 设置编辑器焦点状态
   */
  const setEditorFocus = (focused) => {
    editorHasFocus.value = focused;
  };

  return {
    // 状态
    editorInstance,
    isEditorReady,
    editorHasFocus,
    showCustomPlaceholder,
    currentTag,
    wordCount,

    // 方法
    updateWordCount,
    updateCurrentTag,
    scrollToCursor,
    updateCustomPlaceholder,
    handleContentClick,
    handleContentChange,
    setEditorFocus,
  };
}
